package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCReg {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()){
            Scanner sc = new Scanner(System.in);
            String username;String password;String nickname;String age;
            String sql;PreparedStatement ps;ResultSet r;
            while (true) {
                System.out.println("请输入账号: ");
                username = sc.nextLine();
                System.out.println("请输入密码: ");
                password = sc.nextLine();
                System.out.println("请输入昵称: ");
                nickname = sc.nextLine();
                System.out.println("请输入age: ");
                age = sc.nextLine();
                sql = "select username from userinfo where username=?";
                ps = connection.prepareStatement(sql);
                ps.setString(1, username);
                r = ps.executeQuery();
                if(r.next()){
                    System.out.println("该账号已被注册");
                }else {break;}
            }
            sql = "insert into userinfo(username,password,nickname,age)values(?,?,?,?) ";
            ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ps.setString(3, nickname);
            ps.setString(4, age);
            int i =ps.executeUpdate();
            if (i>0){
                System.out.println("注册成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
